package com.demo2do.fms.admin.web.controller.report.appointment;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.page.Page;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.report.VehicleMileageAppointmentService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.report.VehicleMileageAppointment;

/**
 * 驳运车累计行驶公里数报表预约
 *
 * @author Ark
 *
 */
@Controller
@RequestMapping("vehicle-mileage-appointments")
@MenuSetting("menu-report")
public class VehicleMileageAppointmentController {
    
    private static final Logger logger = LoggerFactory.getLogger(VehicleMileageAppointmentController.class);
    
    @Autowired
    private VehicleMileageAppointmentService appointmentService;

    /**
     * 预约列表
     * 
     * @param orgIds
     * @param page
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle-mileage-appointment")
    public ModelAndView list(@Secure(property = "orgIds") List<Long> orgIds, Page page) {
        
        ModelAndView modelAndView = new ModelAndView("report/appointment/vehicle-mileage/vehicle-mileage-appointment-list");
        
        int totals = appointmentService.count(orgIds);
        if (totals > 0) {
            
            List<VehicleMileageAppointment> appointments = appointmentService.list(orgIds, page.with(totals));
            
            modelAndView.addObject("appointments", appointments);
            modelAndView.addObject("page", page);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 预约详情
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @MenuSetting("submenu-vehicle-mileage-appointment")
    public ModelAndView view(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("report/appointment/vehicle-mileage/vehicle-mileage-appointment-view");
        
        VehicleMileageAppointment appointment = appointmentService.load(VehicleMileageAppointment.class, id);
        
        modelAndView.addObject("appointment", appointment);
        
        return modelAndView;
        
    }
    
    /**
     * 添加预约panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.GET)
    public ModelAndView create(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/appointment/vehicle-mileage/vehicle-mileage-appointment-create");

        modelAndView.addObject("region", login.isRegionUser());
        modelAndView.addObject("orgIds", login.getCurrOrgIds());
        
        return modelAndView;
        
    }
    
    /**
     * 添加预约
     * 
     * @param appointment
     * @param operator
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onCreate(VehicleMileageAppointment appointment, @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            appointmentService.create(appointment, operator);
            
            httpResult.success().message("正在检查数据量，30分钟后可查看是否预约成功。");
            
        } catch (BusinessException e) {
            logger.error("#onCreate() - create failed, business exception={}", e);
            httpResult.fail("500").message("添加预约失败：" + e.getMessage());
        } catch (Exception e) {
            logger.error("#onCreate() - create failed, exception={}", e);
            httpResult.fail("500").message("添加预约失败");
        }
        
        return httpResult;
        
    }
    
    /**
     * 删除预约panel
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public ModelAndView delete(@PathVariable("id") Long id) {
        
        ModelAndView modelAndView = new ModelAndView("report/appointment/vehicle-mileage/vehicle-mileage-appointment-delete");
        
        modelAndView.addObject("id", id);
        
        return modelAndView;
        
    }
    
    /**
     * 删除预约
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onDelete(@PathVariable("id") Long id, 
                                             @Secure(property = "id") Long operator) {
        
        HttpResult httpResult = new HttpResult();
        
        try {
            
            VehicleMileageAppointment appointment = appointmentService.load(VehicleMileageAppointment.class, id);
            appointmentService.delete(appointment, operator);
            
            httpResult.success().message("删除成功");
            
        } catch (Exception e) {
            logger.error("#onDelete() - delete failed, exception={}", e);
            httpResult.fail("500").message("删除失败！" + e.getMessage());
        }
        
        return httpResult;
        
    }
    
}
